package com.wanmao.test.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.wanmao.test.bean.XMBean;
import com.wanmao.test.bean.XZBean;
import com.wanmao.test.bean.YDBean;
import com.wanmao.test.dbhelper.DBOpenHelper;

import java.util.ArrayList;
import java.util.List;



public class XZService {
    private Context context;
    private DBOpenHelper dBOpenHelper;
    private String JDJD;
    public String getJDJD() {
        return JDJD;
    }

    public void setJDJD(String JDJD) {
        this.JDJD = JDJD;
    }

    public XZService(Context context) {
        this.context = context;
        this.dBOpenHelper = new DBOpenHelper(context);
    }

    public void save(XZBean bean) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        db.insert("XZ_TABLE", null, settingValue(bean));
    }


    private ContentValues settingValue(XZBean bean) {
        ContentValues values = new ContentValues();
        values.put("ZJ_ID",bean.getZJ_ID());
        values.put("RWID",bean.getRWID());
        values.put("XZ_ID",bean.getXZ_ID());
        values.put("XZMC",bean.getXZMC());
        values.put("XMSIZE",bean.getXMSIZE());
        values.put("YDSIZE",bean.getYDSIZE());
        values.put("HEGE",bean.getHEGE());
        values.put("BUHEGE",bean.getBUHEGE());
        values.put("BUSHEJI",bean.getBUSHEJI());
        values.put("WEIJIANCHA",bean.getWEIJIANCHA());
        return values;
    }

    public List<XZBean> findAll(String rwid){
        String sql = "select * from XZ_TABLE where RWID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql,new String[]{rwid});
        return toListBean(cursor);
    }

    private List<XZBean> toListBean(Cursor cursor){
        List<XZBean> list=new ArrayList<>();
        while (cursor.moveToNext()){
            XZBean bean=new XZBean();
            bean.setZJ_ID(cursor.getString(cursor.getColumnIndex("ZJ_ID")));
            bean.setRWID(cursor.getString(cursor.getColumnIndex("RWID")));
            bean.setXZ_ID(cursor.getString(cursor.getColumnIndex("XZ_ID")));
            bean.setXZMC(cursor.getString(cursor.getColumnIndex("XZMC")));
            bean.setXMSIZE(cursor.getString(cursor.getColumnIndex("XMSIZE")));
            bean.setYDSIZE(cursor.getString(cursor.getColumnIndex("YDSIZE")));
            bean.setHEGE(cursor.getString(cursor.getColumnIndex("HEGE")));
            bean.setBUHEGE(cursor.getString(cursor.getColumnIndex("BUHEGE")));
            bean.setBUSHEJI(cursor.getString(cursor.getColumnIndex("BUSHEJI")));
            bean.setWEIJIANCHA(cursor.getString(cursor.getColumnIndex("WEIJIANCHA")));
            getNewYDSIZE(cursor.getString(cursor.getColumnIndex("XZ_ID")),bean,cursor.getString(cursor.getColumnIndex("RWID")));
            list.add(bean);
        }

        return list;
    }

    public XZBean updateXMSIZE(String XZID, String XMSIZE) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        String sql="update XZ_TABLE set XMSIZE='"+XMSIZE+"' where XZ_ID='"+XZID+"'";
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor.moveToNext()){
            return toBean(cursor);
        }

        return null;
    }
    public XZBean updateYDSIZE(String XZID, String YDSIZE) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        String sql="update XZ_TABLE set YDSIZE='"+YDSIZE+"' where XZ_ID='"+XZID+"'";
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor.moveToNext()){
            return toBean(cursor);
        }

        return null;
    }
    public XZBean findXZID(String XZID){
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        String sql = "select * from XZ_TABLE where XZ_ID='"+XZID+"'";
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor.moveToNext()){
            return toBean(cursor);
        }
        return null;
    }
    public XZBean findXZIDByRWID(String XZID,String RWID){
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        String sql = "select * from XZ_TABLE where XZ_ID='"+XZID+"' and RWID='"+RWID+"'";
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor.moveToNext()){
            return toBean(cursor);
        }
        return null;
    }
    public XZBean updateXZSIZE(String RWID,String XZID, String HEGE, String BUHEGE, String BUSHEJI) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        String sql="update XZ_TABLE set HEGE='"+HEGE+"',BUHEGE='"+BUHEGE+"',BUSHEJI='"+BUSHEJI+"' where XZ_ID='"+XZID+"' and RWID='"+RWID+"'";
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor.moveToNext()){
            return toBean(cursor);
        }

        return null;
    }

    private XZBean toBean(Cursor cursor) {
        XZBean bean = new XZBean();
        bean.setRWID(cursor.getString(cursor.getColumnIndex("RWID")));
        bean.setXZ_ID(cursor.getString(cursor.getColumnIndex("XZ_ID")));
        bean.setXZMC(cursor.getString(cursor.getColumnIndex("XZMC")));
        bean.setXMSIZE(cursor.getString(cursor.getColumnIndex("XMSIZE")));
        bean.setYDSIZE(cursor.getString(cursor.getColumnIndex("YDSIZE")));
        bean.setHEGE(cursor.getString(cursor.getColumnIndex("HEGE")));
        bean.setBUHEGE(cursor.getString(cursor.getColumnIndex("BUHEGE")));
        bean.setBUSHEJI(cursor.getString(cursor.getColumnIndex("BUSHEJI")));
        bean.setWEIJIANCHA(cursor.getString(cursor.getColumnIndex("WEIJIANCHA")));

        return  bean;

    }

    /**
     * 重新计算要点数
     */
    private void getNewYDSIZE(String XZID,XZBean Fbean,String RWID) {
        XMService xmService=new XMService(this.context);
        List<XMBean> listLocal=xmService.findXZID(XZID,getJDJD());
        YDService ydService=new YDService(this.context);
        XMBean bean;
        List<YDBean> list1;
        List<Integer> noChild=new ArrayList<>();
        int ydsize=0;
        for (int j = 0; j <listLocal.size(); j++) {
            String XMID=listLocal.get(j).getXM_ID();
            list1 = ydService.findXMID(XMID,RWID);
            if(list1.size()<=0){
                noChild.add(j);
            }else{
                ydsize+=list1.size();
            }
            bean = listLocal.get(j);
            bean.setList(list1);
        }

        for (int m = 0; m < noChild.size(); m++) {
            int index = noChild.get(m)-m;
            if(listLocal.size()>=index){
                listLocal.remove(index);
            }
        }
        Fbean.setYDSIZE(ydsize+"");
        Fbean.setXMSIZE(listLocal.size()+"");
    }

}
